import xalglib
import math
import cmath
import sys
import random

def conv(t, l, c, v):
	tt = range(l)
	for i in range(l):
		tt[i] = range(c)
		for k in range(c):
			tt[i][k] = 0
			for j in range(c):
				tt[i][k] = tt[i][k] + float(t[i][j]) * v[j][k]
	return tt

def grav(t, l, c):
	g = range(c)
	for i in range(c):
		g[i] = 0
		for j in range(l):
			g[i] = g[i] + t[j][i]
	return g


def dist2(t1, t2):
	d = 0
	for i in range(len(t1)):
		a = t1[i] - t2[i]
		d = d + a*a
	return d

def reconnait(t1, t2, tx):
        for i in range(len(t1)) :
                for j in range(len(t1[i])) :
                        t1[i][j] = float(t1[i][j])
        for i in range(len(t2)) :
                for j in range(len(t2[i])) :
                        t2[i][j] = float(t2[i][j])
        for i in range(len(tx)) :        
                for j in range(len(tx[i])) :
                        tx[i][j] = float(tx[i][j])

	c = len(t1[0])
	l1 = len(t1)
	l2 = len(t2)
	lx = len(tx)
	info, s2, v = xalglib.pcabuildbasis(t1 + t2, l1 + l2, c)
        print l1, c, len(v), len(v[0])
	tt1 = conv(t1, l1, c, v)
	tt2 = conv(t2, l2, c, v)
	ttx = conv(tx, lx, c, v)	
	g1 = grav(t1, l1, c)
	g2 = grav(t2, l2, c)
	gx = grav(tx, lx, c)
        print dist2(g1, gx)
        print dist2(g2, gx)
	if dist2(g1, gx) <  dist2(g2, gx) :
                return "1"
        return "2"
